//
//  UIButton+Extention.swift
//  sinazd
//
//  Created by apple on 16/3/11.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
extension UIButton {
    //有背景图和图片的按钮
    class func buttonWithImage(imageName:String?,backImageName:String?) ->UIButton {
        
        //实例化一个btn
        let btn = UIButton()
        //设置btn
        if imageName != nil {
            btn.setImage(UIImage(named: imageName!), forState: .Normal)
            btn.setImage(UIImage(named: imageName! + "_highlighted"), forState: .Highlighted)
        }
        
        if backImageName != nil {
        
        btn.setBackgroundImage(UIImage(named: backImageName!), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: backImageName! + "_highlighted"), forState: .Highlighted)
        
        }
        
        btn.sizeToFit()
        
        return btn
        
    }
    //有图片和文字的按钮
    class func buttonWithTitle(title:String,backImageName:String?,textColor:UIColor,fontSize:CGFloat,imageName:String? = nil) ->UIButton {
        
        let btn = UIButton()
        //这是背景图片
        
        //let img = UIImage(named: backImageName)
//        let w = Int (img!.size.width * 0.5)
//        let h = Int (img!.size.height * 0.5)
        
        
        //btn.setBackgroundImage(img?.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
        
        
        if backImageName != nil {
            btn.setBackgroundImage(UIImage(named:backImageName!), forState: .Normal)
        }
        
        if imageName != nil {
            btn.setImage(UIImage(named: imageName!), forState: .Normal)
        }
        
        
        btn.setTitle(title, forState: .Normal)
        btn.setTitleColor(textColor, forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        
        btn.sizeToFit()
        return btn
   
    }
    
    
}